package com.twu.biblioteca;


/**
 * Created by siba on 22/09/14.
 */
public class MainMenu {
    private String options [] = new String [Constants.OPTIONS.length + 1];
    private String choice;
    private User user;

    public void setUser(User user) {
        this.user = user;
    }

    public void initialiseMenuOptions(){
        for(int count= 0; count < Constants.OPTIONS.length; count++){
            options[count] = Constants.OPTIONS[count];
        }
        if(user != null){
            options[Constants.OPTIONS.length] = Constants.OPTION_DISPLAY_USR_INFO;
        }else {
            options[Constants.OPTIONS.length] = Constants.OPTION_LOGIN;
        }
    }

    public void printMainMenu(){
        initialiseMenuOptions();
        new BibliotecaMessages(System.out).printMessage(Constants.CHOOSE_OPTION_MESSAGE);

        for(int count = 0; count < options.length; count++)
            System.out.println((count+1) + ". " + options[count]);

        new BibliotecaMessages(System.out).printMessage(Constants.QUIT_OPTION_MESSAGE);
        readUserInput();
    }

    public void readUserInput(){
        choice = UserInput.readUserInput();
        int choiceFromList = Constants.NOT_VALID;
        try{
          choiceFromList = Integer.parseInt(choice);
        }catch (NumberFormatException nfe){
            new BibliotecaMessages(System.out).printMessage(Constants.NOT_VALID_MESSAGE);
        }
        checkChoice(choiceFromList);
    }

    public void checkChoice(int choiceFromList){
            switch (choiceFromList) {
                case Constants.LIST_BOOKS:
                    listBooks();
                    break;
                case Constants.LIST_MOVIES:
                    listMovies();
                    break;
                case Constants.CHECKOUT_A_BOOK:
                    checkoutBook();
                    break;
                case Constants.RETURN_A_BOOK:
                    returnBook();
                    break;
                case Constants.CHECKOUT_A_MOVIE:
                    checkoutMovie();
                    break;
                case Constants.DISPLAY_OR_LOGIN:
                    if(user != null){
                        user.displayUserInfo();
                    }else{
                        user = new UserLogin().login();
                    }
                    break;
                case Constants.QUIT:
                    new BibliotecaMessages(System.out).printMessage(Constants.QUIT_MESSAGE);
                    System.exit(0);
                default:
                    new BibliotecaMessages(System.out).printMessage(Constants.TRY_AGAIN_MESSAGE);
            }
    }

    private void checkoutMovie() {
        Movie movie = new Movie(UserInput.getMovieDetailsFromUser());
        Movie.checkoutMovieFromLibrary(movie);
    }

    private void returnBook() {
        if(user != null) {
            Book book = new Book(UserInput.getBookDetailsFromUser());
            int result = Book.returnBookToLibrary(book);
            if (result == Constants.SUCCESS){
                user.removeFromBooksCheckedOut(book);
            }
        }else{
            user = new UserLogin().login();
        }

    }

    private void checkoutBook() {
        if(user != null) {
            Book book = new Book(UserInput.getBookDetailsFromUser());
            int result = Book.checkoutBookFromLibrary(book);
            if(result == Constants.SUCCESS){
                user.addToBooksCheckedOut(book);
            }
        }else{
            user = new UserLogin().login();
        }
    }

    public void listBooks(){
        int count = 1;
        for(Book book: BookInformation.getLibraryBooks()){
            if(!book.getIsCheckedOut())
                new BibliotecaMessages(System.out).printMessage(count++ +". Title: " + book.getTitle() + "\t\tAuthor: " +
                        book.getAuthor() + "\t\tYear Published: " + book.getYearPublished());
        }
    }

    private void listMovies() {
        int count = 1;
        for(Movie movie: MovieInformation.getLibraryMovies()){
            if(!movie.getIsCheckedOut())
                new BibliotecaMessages(System.out).printMessage(count++ +". Name: " + movie.getName() + "\t\tDirector: " +
                        movie.getDirector() + "\t\tYear released: " + movie.getYear() +
                        "\t\tRating: " + movie.getRating());
        }
    }

 }

